package com.sharkburner.aeris.ui.util;

import java.io.Serializable;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.sharkburner.common.component.AddButton;
import com.sharkburner.common.component.BodyComposition;
import com.sharkburner.common.component.CustomMenuComponent;
import com.sharkburner.common.component.EditButton;
import com.sharkburner.common.component.RemoveButton;
import com.sharkburner.common.listener.AddListener;
import com.sharkburner.common.listener.ButtonListener;
import com.sharkburner.common.listener.EditListener;
import com.sharkburner.common.listener.GridViewListener;
import com.sharkburner.common.listener.RemoveListener;
import com.vaadin.ui.Layout;

/**
 * Composition Builder, Build a compostion from its listeners
 * 
 * @author inartea
 * 
 */
@Component
@Scope("prototype")
public class CompositionBuilder implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private BodyComposition bodyLayout;

	/**
	 * Create body composition dependent by component listeners
	 * 
	 * @param body
	 * @return
	 */
	public BodyComposition getBodyComposition(Layout body) {
		Class<?>[] listeners = body.getClass().getInterfaces();
		bodyLayout = new BodyComposition();

		for (Class<?> listener : listeners) {
			CustomMenuComponent button = null;

			if (listener == AddListener.class) {
				button = new AddButton();
			} else if (listener == RemoveListener.class) {
				button = new RemoveButton();
			} else if (listener == EditListener.class) {
				button = new EditButton();
			} else if (listener == GridViewListener.class) {
				/*
				 * if (listener == ListViewListener.class) { button = new
				 * ListViewButton(); } else if (listener ==
				 * DetailViewListener.class) { button = new DetailViewButton();
				 * }
				 */
			}

			if (button != null) {
				button.setListener((ButtonListener) body);

				if (listener.isInstance(GridViewListener.class)) {
					bodyLayout.getMenuBar().addLeft(
							(com.vaadin.ui.Component) button);
				} else {
					bodyLayout.getMenuBar().addRight(
							(com.vaadin.ui.Component) button);
				}
			}
		}

		bodyLayout.setBody(body);

		return bodyLayout;
	}
}
